package com.css.fxfzdzzh.modules.reportManagement.controller;

import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.reportManagement.entity.Report;
import com.css.fxfzdzzh.modules.reportManagement.param.PageQueryParam;
import com.css.fxfzdzzh.modules.reportManagement.param.ReportCheckQueryParam;
import com.css.fxfzdzzh.modules.reportManagement.service.ReportCheckService;
import com.css.fxfzdzzh.util.PlatformPageUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年05月12日  11:07
 */
@RestController
@RequestMapping("/reportCheck")
public class ReportCheckController {

    @Resource
    ReportCheckService reportCheckService;
    /**
     *  报告审核分页查询
     */
    @GetMapping("/page")
    public RestResponse page(ReportCheckQueryParam param, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=reportCheckService.page(param,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /***
     * 文件下载
     * @return
     */
    @PostMapping("/downloadFile")
    public void downloadFile(@RequestBody Map<String,Object> map, HttpServletResponse res){
        String localReport =(String) map.get("localReport");
        String reportSavePath =(String) map.get("reportSavePath");
        reportCheckService.downloadFile(localReport,reportSavePath,res);
    }

    /***
     * 报告审核
     * @return
     */
    @PostMapping("/audit")
    public RestResponse audit(@RequestBody Report report){
        reportCheckService.audit(report);
        return ResponseCreator.createSuccessResponse("审核完成");
    }

    /***
     * 审核不通过上传附件
     * @param file
     * @return
     */
    @PostMapping("/uploadAttachment")
    public RestResponse uploadAttachment(@RequestParam("file") MultipartFile file, @RequestParam("reportId") String reportId,@RequestParam("reportType") String reportType) throws IOException {
        return reportCheckService.uploadAttachment(file,reportId,reportType);
    }

    /***
     * 审核页面上传了附件但没保存点击了取消时调用删除之前上传的附件
     * @param
     * @return
     */
    @PostMapping("/cancelAudit")
    public void cancelAudit(@RequestBody Map<String,String> map){
        reportCheckService.cancelAudit(map);
    }
//    /***
//     * 下载不通过时上传的附件
//     * @param map
//     * @return
//     */
//    @PostMapping("/downLoadReFile")
//    public void downLoadReFile(@RequestBody Map<String,Object> map, HttpServletResponse res) throws IOException {
//         reportCheckService.downLoadReFile(map,res);
//    }

    /**
     *  审核记录分页查询
     */
    @GetMapping("/recordPage")
    public RestResponse recordPage(ReportCheckQueryParam param, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=reportCheckService.recordPage(param,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

}
